package com.bigwork.service.impl;

//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigwork.dto.user.UserLoginRequest;
import com.bigwork.dto.user.UserRegistrationRequest;
import com.bigwork.dto.user.UserResponse;
import com.bigwork.dto.user.UserUpdateRequest;
import com.bigwork.entity.User;
import com.bigwork.exception.ResourceNotFoundException;
import com.bigwork.mapper.UserMapper;
import com.bigwork.service.UserService;
import com.bigwork.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final PasswordEncoder passwordEncoder;
    private final JwtUtil jwtUtil;
    private final AuthenticationManager authenticationManager;
    private final UserDetailsService userDetailsService;

    @Override
    @Transactional
    public UserResponse register(UserRegistrationRequest request) {
        User existingUser = userMapper.selectByUsername(request.getUsername());
        if (existingUser != null) {
            throw new IllegalStateException("用户名 '" + request.getUsername() + "' 已存在");
        }
        User user = new User();
        user.setUsername(request.getUsername());
        user.setPassword(passwordEncoder.encode(request.getPassword()));
        userMapper.insert(user);
        return UserResponse.fromEntity(user);
    }

    @Override
    public String login(UserLoginRequest request) {
        authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(request.getUsername(), request.getPassword())
        );
        final UserDetails userDetails = userDetailsService.loadUserByUsername(request.getUsername());
        return jwtUtil.generateToken(userDetails);
    }

    @Override
    public UserResponse findUserById(Long id) {
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new ResourceNotFoundException("未找到ID为 " + id + " 的用户");
        }
        return UserResponse.fromEntity(user);
    }

    @Override
    @Transactional
    public UserResponse updateUser(Long id, UserUpdateRequest request) {
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new ResourceNotFoundException("未找到ID为 " + id + " 的用户");
        }

        if (request.getPassword() != null && !request.getPassword().isEmpty()) {
            user.setPassword(passwordEncoder.encode(request.getPassword()));
        }
        userMapper.updateById(user);
        return UserResponse.fromEntity(user);
    }
}